load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
load "~/work/mylib/callvar.ncl" 
load "lib_boxplot.ncl" 

begin

  tp = "spar" 

  fna = "boxdata_spar_obs.nc" 
  fnb = "boxdata_spar_cam5bn.nc" 
  fnc = "boxdata_spar_cam5lp.nc" 

  fla = addfile(fna,"r") 
  flb = addfile(fnb,"r") 
  flc = addfile(fnc,"r") 

  obspa = fla->BOX_210K
  obspb = fla->BOX_220K
  obspc = fla->BOX_230K
  obspd = fla->BOX_240K
 
  bnpa = flb->BOX_210K
  bnpb = flb->BOX_220K
  bnpc = flb->BOX_230K
  bnpd = flb->BOX_240K
 
  lppa = flc->BOX_210K
  lppb = flc->BOX_220K
  lppc = flc->BOX_230K
  lppd = flc->BOX_240K
 
;;...........................................................................
;; data for plot 
;;...........................................................................
  xval = new((/4,3,6/),"float",-999.)

  do is = 0,5
     xval(0,0,is) = obspa(is)
     xval(1,0,is) = obspb(is)
     xval(2,0,is) = obspc(is)
     xval(3,0,is) = obspd(is)
     xval(0,1,is) = bnpa(is)
     xval(1,1,is) = bnpb(is)
     xval(2,1,is) = bnpc(is)
     xval(3,1,is) = bnpd(is)
     xval(0,2,is) = lppa(is)
     xval(1,2,is) = lppb(is)
     xval(2,2,is) = lppc(is)
     xval(3,2,is) = lppd(is)
  end do 


  nt = 4 
  ns = 3 

;;...........................................................................
;; reorder 
;;...........................................................................
  pp = new((/ns*nt,6/),"float")

  isp = 0

  do is = 0, ns-1 ;; exp first  
  do it = 0, nt-1 ;; temp band second 
     ;;print("isn,iep,isp: "+isn+" "+iep+" "+isp) 
     ;;       isn,iep,isp: 4 3 20
     pp(isp,:) = xval(it,is,:)
     isp = isp + 1
  end do
  end do

  x = (/210., 220., 230., 240./)		
  
  ppx = new( ns*nt, float )

  do iep=0,ns-1
  do isn=0,nt-1
      ppx(iep*nt+isn) = 5 + iep*(ns*0.9) + isn*0.4
  end do
  end do 

;;...........................................................................
;; wks
;;...........................................................................
  wks = gsn_open_wks("pdf","box_"+tp)

;;...........................................................................
;; res
;;...........................................................................
  res            = True
  res@gsnDraw    = False        ; do not draw picture
  res@gsnFrame   = False        ; do not advance frame

  res@tiMainString = "SPARTICUS"
  res@tiMainFontHeightF  = 0.03
  ;;res@tmXBLabels = (/" ","205K-215K"," "," ","215K-225K"," "," ","225K-235K"," "," ","235K-245K"," "/) ; labels for each box
  res@tmXBLabels = (/"T1","T2","T3","T4","T1","T2","T3","T4","T1","T2","T3","T4"/) 
  res@tmXBLabelFontHeightF = 0.016

  ;;res@trXMinF = 3.5  
  ;;res@trXMaxF = 20
  res@trYMinF = -200.
  res@trYMaxF = 1800.

;;...........................................................................
;; resources for polylines that draws the boxes
;;...........................................................................
  llres                   = True			
  llres@gsLineThicknessF  = 2.5                 ; line thickness 


;;------------------------------------------------------------------------- 
;; change plot size 
;;------------------------------------------------------------------------- 
 ;res@mpShapeMode  = "FreeAspect"
  res@vpWidthF      = 0.8
  res@vpHeightF     = 0.8*0.618

;;...........................................................................
;; resources that control color and width of boxes
;;...........................................................................
  opti          = True			
  opti@boxWidth = 0.32 ;;1.6
  opti@boxColors = (/"black","black","black","black","blue","blue","blue","blue","red","red","red","red"/) ;;,"green","black"/) ; Color of box(es)
  ;;res@gsnMaximize = True                        ; Maximize box plot in frame.

  plot = new(4,graphic)   

  plot(0) = boxplot2(wks,ppx,pp,opti,res,llres)
  ;;plot(1) = boxplot(wks,x,zval,opti,res,llres)


;;...........................................................................
;; add some polymarkers
;;...........................................................................
  mres               = True                     ; marker mods desired
  mres@gsMarkerIndex = 1                        ; polymarker style
  mres@gsMarkerSizeF = 50.                      ; polymarker size
  mres@gsMarkerColor = "black"                    ; polymarker color

  xx1 = ppx(0)
  xx2 = ppx(1)
  xx3 = ppx(2)
  xx4 = ppx(3)

  yy1 = xval(0,0,5)                               ; polymarker locations
  yy2 = xval(1,0,5)
  yy3 = xval(2,0,5)
  yy4 = xval(3,0,5)

  dum1 = gsn_add_polymarker(wks,plot(0),xx1,yy1,mres) 
  dum2 = gsn_add_polymarker(wks,plot(0),xx2,yy2,mres) 
  dum3 = gsn_add_polymarker(wks,plot(0),xx3,yy3,mres) 
  dum4 = gsn_add_polymarker(wks,plot(0),xx4,yy4,mres) 

  xx1 = ppx(4)
  xx2 = ppx(5)
  xx3 = ppx(6)
  xx4 = ppx(7)

  yy1 = xval(0,1,5)                               ; polymarker locations
  yy2 = xval(1,1,5)
  yy3 = xval(2,1,5)
  yy4 = xval(3,1,5)

  eum1 = gsn_add_polymarker(wks,plot(0),xx1,yy1,mres) 
  eum2 = gsn_add_polymarker(wks,plot(0),xx2,yy2,mres) 
  eum3 = gsn_add_polymarker(wks,plot(0),xx3,yy3,mres) 
  eum4 = gsn_add_polymarker(wks,plot(0),xx4,yy4,mres) 
  
  xx1 = ppx(8) 
  xx2 = ppx(9) 
  xx3 = ppx(10)
  xx4 = ppx(11) 

  yy1 = xval(0,2,5)                               ; polymarker locations
  yy2 = xval(1,2,5)
  yy3 = xval(2,2,5)
  yy4 = xval(3,2,5)

  fum1 = gsn_add_polymarker(wks,plot(0),xx1,yy1,mres)
  fum2 = gsn_add_polymarker(wks,plot(0),xx2,yy2,mres)
  fum3 = gsn_add_polymarker(wks,plot(0),xx3,yy3,mres)
  fum4 = gsn_add_polymarker(wks,plot(0),xx4,yy4,mres)
  
  mres@gsMarkerColor = "navy blue"              ; change color
  mres@gsMarkerIndex = 1                        ; change style
  mres@gsMarkerSizeF = 40.                      ; change size

;;  dum4 = gsn_add_polymarker(wks,plot,-3.,xi-4.,mres) 
;;  dum5 = gsn_add_polymarker(wks,plot,-1.,xi2-2.,mres)
;;  dum6 = gsn_add_polymarker(wks,plot,1.,xi3-1.,mres) 

; draw panel with white space added
  resP                 = True
  resP@gsnPanelYWhiteSpacePercent = 5
  resP@gsnPanelXWhiteSpacePercent = 5
  gsn_panel(wks,plot,(/2,1/),resP)

  
end




 
